// index.js
import {
    categoryData,
    categoryCurrentPageData
} from "../../request/request"
Page({

    onLoad: async function () {
        await categoryData().then(res => {
            if (res.errno != 0) return
            this.setData({
                currentCategory: res.data.currentCategory,
                categoryList: res.data.categoryList
            })
            let tempItems = this.data.categoryList.map((item) => {
                return {
                    id: item.id,
                    text: item.name,
                    banner_url: item.banner_url,
                    front_name: item.front_name
                }
            })
            this.setData({
                items: tempItems
            })

        })
        this.getCurrentPage(this.data.items[0].id)
    },
    onShow: function () {
        //获取当前页面的tabbar对象,设置他的active值为当前页面索引，解决tabbar闪烁乱跳问题
        let tabbarObj = this.getTabBar()
        tabbarObj.setData({
            active: 3
        })
    },
    data: {
        currentIndex: 0, //当前点击索引
        categoryList: [], //分类页左侧数组
        currentCategory: {}, //分类页第一项
        items: [{
            text: ""
        }, {}],
        subCategoryList: [], //当前页图片和数据

    },
    //发起请求当前页数据
    getCurrentPage(id) {
        categoryCurrentPageData(id).then(res => {
            console.log(res);
            if (res.errno != 0) return
            this.setData({
                subCategoryList: res.data.currentCategory.subCategoryList
            })
            // console.log(this.data.subCategoryList);
        })
    },
    //左侧导航点击
    onClickNav({
        detail = {}
    }) {
        // console.log(detail);
        this.setData({
            currentIndex: detail.index
        });
        // console.log(this.data.items[detail.index].id);
        //获取当前页数据
        this.getCurrentPage(this.data.items[detail.index].id)
    },
    //聚焦搜索框
    focusSearch() {
        wx.navigateTo({
            url: '/pages/popup/popup',
        })
    }


})